function FFMCLoad() {
	var nocontacts = JSON.stringify({"names":[],"lownames":[],"notes":[]});
	var contactsText = localStorage.getItem("FFMContactsOrion");
	//return nocontacts;
	if (!contactsText) {
		return nocontacts;
	}
	else {
		return contactsText;
	}
}

function FFMCSave() {
	localStorage.setItem("FFMContactsOrion", JSON.stringify(FFMCcontacts));
}

var FFMCcontacts; 
var contactbutton;
var name;

function FFMCsetattribs(action) {
	if (action === "remove") {
		contactbutton.value = "Remove from contacts";
		contactbutton.setAttribute('onclick',"FFMCremovecontact(\"" + name + "\")");
	} else {
		contactbutton.value = "Add to contacts";
		contactbutton.setAttribute('onclick',"FFMCaddcontact(\"" + name + "\")");
	}
}

function FFMCaddcontact(name) {
	var note = prompt("Enter any info you want to associate with this pilot.","");
	if (note === null) {
		return;
	}
	if (note === "") {
		note = "No info provided";
	}
	
	setTimeout(function() {
		if (FFMCcontacts.lownames.indexOf(name.toLowerCase()) === -1) {
			FFMCcontacts.names.push(name);
			FFMCcontacts.lownames.push(name.toLowerCase());
			FFMCcontacts.notes.push(note);
			FFMCSave();
		}
	},1);
	
	contactbutton.value = "Added!";
	setTimeout(function() {
		FFMCsetattribs("remove");
	},1000);
}

function FFMCremovecontact(name) {
	setTimeout(function() {
		var index = FFMCcontacts.lownames.indexOf(name.toLowerCase());
		if (index !== -1) {
			FFMCcontacts.names.splice(index,1);
			FFMCcontacts.lownames.splice(index,1);
			FFMCcontacts.notes.splice(index,1);
			FFMCSave();
		}
	},1);
	
	contactbutton.value = "Removed!";
	setTimeout(function() {
		FFMCsetattribs("add");
	},1000);
}

function FFMCrun() {
	var field = document.getElementById('recipient2');
	name = unescape(window.location.href.split('&')[0].split('=')[1]);
	if (!field) return;

	if (document.links[0] && document.links[0].href.indexOf("profile.php?id=") !== -1) {
		field.parentNode.innerHTML += " <input type='button' style='margin-right:3px;margin-top:15px;display:inline' id='contactbutton' value='' onclick='return;'><form style='display:inline' name='fdipl_add' action='diplomacy.php' target='hidefrm' method='post'><input id='dipl_name' type='hidden' name='name' value=''><input type='hidden' name='type' value=''><input type='hidden' name='action' value='add'><button style='width:50px; margin:3px; display:inline; color:#00FF00' onclick='document.fdipl_add.type.value=\"friend\"; document.fdipl_add.submit(); '>friend</button><button style='width:50px; margin:3px; display:inline; color:#FF0000' onclick='document.fdipl_add.type.value=\"foe\"; document.fdipl_add.submit();'>foe</button></form> <iframe name='hidefrm' style='display:none' width='0' height='0'></iframe>";	
		document.getElementById('dipl_name').value = name;
	} else {		
		field.parentNode.innerHTML += " <input type='button' style='margin-top:15px;display:inline' id='contactbutton' value='' onclick='return;'>";
	}

	contactbutton = document.getElementById('contactbutton');
	FFMCcontacts = JSON.parse(FFMCLoad());

	if (FFMCcontacts.lownames.indexOf(name.toLowerCase()) === -1) {
		FFMCsetattribs("add");
	} else {
		FFMCsetattribs("remove");
	}
}

FFMCrun();
